<template>
  <el-upload
    class="avatar-uploader"
    :action="imgUpload"
    :show-file-list="false"
    :headers="{ 'admin-token': token }"
    :on-success="handleAvatarSuccess"
    name="img"
  >
    <img v-if="imgUrl" :src="img" class="avatar">
    <i v-else class="el-icon-plus avatar-uploader-icon" style="border: 2px dashed #ecece0;" />
  </el-upload>
</template>

<script>
import { getToken } from '@/utils/auth'
import { imgUpload } from '@/routerConfig'
import { baseUrl } from '@/routerConfig'
export default {
  props: {
    // eslint-disable-next-line vue/require-default-prop
    imgUrl: {
      type: String
    }
  },
  data() {
    return {
      imgUpload,
      token: getToken(),
      baseUrl
    }
  },
  computed: {
    img() {
      return this.imgUrl === '' ? '' : this.baseUrl + this.imgUrl
    }
  },
  methods: {
    handleAvatarSuccess(e) {
      this.$emit('upload', e.data)
    }
  }
}
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 100%;
    display: block;
  }
</style>
